/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>

*/
package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
)

var updateConfig bool

// serviceCmd represents the service command
var serviceCmd = &cobra.Command{
	Use:   "service",
	Short: "service management such as start, stop, restart, deploy, update, rollback, status, uninstall etc",
}

var showCommand = &cobra.Command{
	Use:   "show",
	Short: "service show",
	Run:   showService,
}

var startCommand = &cobra.Command{
	Use:   "start",
	Short: "service start",
	Run:   startService,
}

var stopCommand = &cobra.Command{
	Use:   "stop",
	Short: "service stop",
	Run:   stopService,
}

var restartCommand = &cobra.Command{
	Use:   "restart",
	Short: "service restart",
	Run:   restartService,
}

var deployCommand = &cobra.Command{
	Use:   "deploy",
	Short: "service deploy",
	Run:   deployService,
}

var updateCommand = &cobra.Command{
	Use:   "update",
	Short: "service update",
	Run:   updateService,
}

var rollbackCommand = &cobra.Command{
	Use:   "rollback",
	Short: "service rollback",
	Run:   rollbackService,
}

var statusCommand = &cobra.Command{
	Use:   "status",
	Short: "service status",
	Run:   statusService,
}

var uninstallCommand = &cobra.Command{
	Use:   "uninstall",
	Short: "service uninstall",
	Run:   uninstallService,
}

func showService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()
	fmt.Printf("服务%s", srv.Describ())
}

func startService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()
	err := srv.Start()
	if err != nil {
		fmt.Printf("%q\n", err)
	}
}

func stopService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()
	err := srv.Stop()
	if err != nil {
		fmt.Printf("%q\n", err)
	}
}

func restartService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()
	err := srv.Restart()
	if err != nil {
		fmt.Printf("%q\n", err)
	}
}

func deployService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()
	err := srv.Deploy()
	if err != nil {
		fmt.Printf("%q\n", err)
	}
}

func updateService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()
	err := srv.Update(updateConfig)
	if err != nil {
		fmt.Printf("%q\n", err)
	}
}

func rollbackService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()
	err := srv.Rollback(updateConfig)
	if err != nil {
		fmt.Printf("%q\n", err)
	}
}

func statusService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()

	running := srv.Status()
	if running {
		fmt.Printf("%s服务正在运行中\n", srv.SrvName())
	} else {
		fmt.Printf("%s服务未运行", srv.SrvName())
	}
}

func uninstallService(cmd *cobra.Command, args []string) {
	generalSrv := msrvConfig.Service
	srv := generalSrv.Action()

	err := srv.Uninstall()
	if err != nil {
		fmt.Printf("%q\n", err)
	}
}

func init() {
	rootCmd.AddCommand(serviceCmd)
	serviceCmd.AddCommand(showCommand)
	serviceCmd.AddCommand(startCommand)
	serviceCmd.AddCommand(stopCommand)
	serviceCmd.AddCommand(restartCommand)
	serviceCmd.AddCommand(deployCommand)
	serviceCmd.AddCommand(updateCommand)
	serviceCmd.AddCommand(rollbackCommand)
	serviceCmd.AddCommand(statusCommand)
	serviceCmd.AddCommand(uninstallCommand)
	updateCommand.Flags().BoolVarP(&updateConfig, "config-file", "", false, "是否更新配置文件")
	rollbackCommand.Flags().BoolVarP(&updateConfig, "config-file", "", false, "是否回滚配置文件")

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// serviceCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// serviceCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
